import React, { useState } from 'react';
import './global.less';
const Person = (props) => {
  //在子组件内部,修改了父组件中的值,从而完成了子组件内部向父组件通信
  const { setCount } = props;
  const handle = () => {
    //函数式更新
    setCount((count) => count + 1);
  };
  return (
    <>
      <div className='person-main'>
        <button onClick={handle}>点击+1</button>
      </div>
    </>
  );
};

export default function CommTwo() {
  //子传父,方法调用.在父组件定义一个方法,传给子组件,子组件用props接收
  const [count, setCount] = useState(0);
  return (
    <>
      <div className='comm-main'>
        <h1>这是父组件</h1>
        <div>计数值:{count}</div>
        <Person setCount={setCount} />
      </div>
    </>
  );
}
